package io.github.biezhi.elves.utils;

import java.io.File;
import java.io.IOException;
import java.awt.Rectangle;
import javax.imageio.ImageIO;
import org.openqa.selenium.Point;
import java.awt.image.BufferedImage;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.internal.WrapsDriver;

/**
 * 使用WebDriver对特定的WebElement截图
 * @version V1.0
 */
public class CreateElementScreenshot {
	public static File captureElement(WebElement element) throws Exception {
		WrapsDriver wrapsDriver = (WrapsDriver) element;
		// 截图整个页面
		File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
		BufferedImage img = ImageIO.read(screen);
		// 获得元素的高度和宽度
		int width = element.getSize().getWidth();
		int height = element.getSize().getHeight();
		// 创建一个矩形使用上面的高度，和宽度
		Rectangle rect = new Rectangle(width, height);
		// 得到元素的坐标
		Point p = element.getLocation();
		BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width, rect.height);
		// 存为png格式
		ImageIO.write(dest, "png", screen);
		return screen;
	}
	
	public static File captureElement(WebElement element,int width) throws Exception {
		WrapsDriver wrapsDriver = (WrapsDriver) element;
		// 截图整个页面
		File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
		BufferedImage img = ImageIO.read(screen);
		// 获得元素的高度和宽度
//		int width = element.getSize().getWidth();
		int height = element.getSize().getHeight();
		// 创建一个矩形使用上面的高度，和宽度
		Rectangle rect = new Rectangle(width, height);
		// 得到元素的坐标
		Point p = element.getLocation();
		BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width, rect.height);
		// 存为png格式
		ImageIO.write(dest, "png", screen);
		return screen;
	}
	
	/**
	 * 获取图像宽和高
	 * @param file
	 * @return
	 */
	public static BufferedImage getImageWidthAndHeight(String file){
		File fileOne = new File(file);
		BufferedImage imageOne;
		try {
			imageOne = ImageIO.read(fileOne);
			return imageOne;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 两张图合并成一张图(两张图的宽度必须一致)
	 * 
	 * @param filePathOne
	 * @param filePathTwo
	 * @param mergeImgFilePath
	 */
	public static void mergeImage(String filePathOne,String filePathTwo,String mergeImgFilePath){
		try {
			// 读取第一张图片
			File fileOne = new File(filePathOne);
			BufferedImage ImageOne = ImageIO.read(fileOne);
			int width = ImageOne.getWidth();
			// 图片宽度
			int height = ImageOne.getHeight();
			// 图片高度
			// 从图片中读取RGB
			int[] ImageArrayOne = new int[width * height];
			ImageArrayOne = ImageOne.getRGB(0, 0, width, height, ImageArrayOne, 0, width);
			// 对第二张图片做相同的处理
			File fileTwo = new File(filePathTwo);
			BufferedImage ImageTwo = ImageIO.read(fileTwo);
			int widthTwo = ImageTwo.getWidth();
			// 图片宽度
			int heightTwo = ImageTwo.getHeight();
			int[] ImageArrayTwo = new int[widthTwo * heightTwo];
			ImageArrayTwo = ImageTwo.getRGB(0, 0, widthTwo, heightTwo, ImageArrayTwo, 0, widthTwo);
			// 生成新图片
			// BufferedImage ImageNew = new BufferedImage(width * 2, height,
			// BufferedImage.TYPE_INT_RGB);
			BufferedImage ImageNew = new BufferedImage(width, (heightTwo+34), BufferedImage.TYPE_INT_RGB);
			ImageNew.setRGB(0, 0, width, height, ImageArrayOne, 0, width);
			// 设置左半部分的RGB
			// ImageNew.setRGB(width, 0, width, height, ImageArrayTwo, 0,
			// width);// 设置右半部分的RGB
			// ImageNew.setRGB(0, height, width,
			// ImageOne.getHeight()+ImageTwo.getHeight(), ImageArrayTwo, 0,
			// width);// 设置右半部分的RGB
			ImageNew.setRGB(0, height, width, heightTwo, ImageArrayTwo, 0, width);
			// 设置右半部分的RGB
			File outFile = new File(mergeImgFilePath);
			ImageIO.write(ImageNew, "png", outFile);
			// 写图片
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
